<template>
    <div class="header">
        <router-link to='/' tag='div' class="header-abs" v-show = 'showAbs'>
            <div class="iconf back-icon">&#xe622;</div>
        </router-link>
        <div class="header-fixed" :style="opacityStyle">
            {{sightName}}
        </div>
    </div>
</template>
<script>
export default {
  name: 'Gallary',
  props: {
    sightName: String
  },
  data () {
    return {
      showAbs: true,
      opacityStyle: {
        opacity: 0
      }
    }
  },
  methods: {
    handleScroll () {
      console.log(document.documentElement.scrollTop)
      const top = document.documentElement.scrollTop
      if (top > 60) {
        this.showAbs = false
        let opacity = top / 140
        opacity = opacity > 1 ? 1 : opacity
        this.opacityStyle = {
          opacity
        }
      } else {
        this.showAbs = true
        this.opacityStyle = {
          opacity: 0
        }
      }
    }
  },
  // 页面进来都会执行,因为exclude了，所以我们要用mounted取代activated
  mounted () {
    console.log('?')
    window.addEventListener('scroll', () => {
      this.handleScroll()
    })
  },
  // 全局事件的解绑在 deactivated函数进行
  deactivated () {
    window.removeEventListener('scroll', () => {})
  }
}
</script>
<style lang="stylus" scoped>
@import "~styles/variables"
.header-abs
    position :fixed
    top:.2rem
    left:.2rem
    background :rgba(0,0,0,0.4)
    width:.8rem
    height:.8rem
    border-radius:.4rem
    color:#fff
    z-index:99
    line-height :.8rem
    text-align :center
    .back-icon
        font-size:.4rem
.header-fixed
    color:#fff
    font-size:.32rem
    position :fixed
    top:0
    left:0
    right:0
    line-height:$navHeight
    text-align:center
    background :$bgColor
    z-index: 98
</style>
